Skip to content

add lib instrumentation for all Django middleware spans#4624

Open
weizhikuan wants to merge 6 commits into
open-telemetry:mainfrom
weizhikuan:i3723
Open

add lib instrumentation for all Django middleware spans#4624
weizhikuan wants to merge 6 commits into
open-telemetry:mainfrom
weizhikuan:i3723

Conversation

@weizhikuan

@weizhikuan weizhikuan commented May 24, 2026

Copy link
Copy Markdown

Description

Add opt-in per-middleware span creation for Django instrumentation. When is_middleware_spans_enabled=True is passed to DjangoInstrumentor().instrument(), each middleware in settings.MIDDLEWARE produces an INTERNAL span nested under the HTTP server span. (sql commenter is intentionally ruled out from the instrumentation)

Covers all three Django middleware categories:

  • MiddlewareMixin subclasses (single class-level wrap)
  • Pure new-style classes (per-class wrap on call)
  • Function-based factories (wrap the factory to return a traced callable)

Async dispatch is handled: mixin middleware defers to acall when
async_mode/is_async is set; new-style call wrappers detect coroutine
returns; function factories check the inner callable.

Fixes #3723

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@weizhikuan weizhikuan marked this pull request as draft May 24, 2026 02:23
@weizhikuan weizhikuan changed the title add lib instrumentation for all Django middlewares add lib instrumentation for all Django middleware spans May 24, 2026
@weizhikuan weizhikuan marked this pull request as ready for review June 4, 2026 14:07
@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Jun 4, 2026
@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Hi @weizhikuan , thank you for this. Please could you fill in more details of the PR description/template?

@tammy-baylis-swi tammy-baylis-swi requested a review from a team June 4, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

Support for Django middleware instrumentation

2 participants